package com.ruoyi.client.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.client.domain.entity.BlogCategory;
import com.ruoyi.client.mapper.BlogCategoryMapper;
import com.ruoyi.client.service.IBlogCategoryService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("client/user/blog-category")
public class BlogCategoryController extends BaseController {
    @Autowired
    IBlogCategoryService categoryService;
    @Autowired
    BlogCategoryMapper mapper;
    //用户端

    //管理端
    @GetMapping("/list")
    public TableDataInfo<BlogCategory> list(PageQuery pageQuery){
        TableDataInfo<BlogCategory> list = categoryService.selectBlogCategoryList(pageQuery.getParams(),pageQuery);
        return list;
    }
    @GetMapping("/all")
    public R<List<BlogCategory>> all(){
        List<BlogCategory> list = categoryService.list(Wrappers.<BlogCategory>lambdaQuery().ne(BlogCategory::getStatus, 0));
        return R.ok(list);
    }
    @DeleteMapping("/{categoryIds}")
    public R<Void> remove(@PathVariable Long[] categoryIds) {
        categoryService.deleteArticleCategoryByIds(categoryIds);
        return R.ok();
    }
    @PutMapping
    public R<Void> edit(@Validated @RequestBody BlogCategory blogCategory) {
        if (categoryService.exists(Wrappers.<BlogCategory>lambdaQuery().eq(BlogCategory::getName,blogCategory.getName()).ne(BlogCategory::getName,blogCategory.getName()))) {
            return R.fail("修改分类'" + blogCategory.getName() + "'失败，分类已存在");
        }
        categoryService.updateById(blogCategory);
        return R.ok();
    }
    @GetMapping(value = "/{categoryId}")
    public R<BlogCategory> getInfo(@PathVariable Long categoryId) {
        return R.ok(categoryService.getById(categoryId));
    }
    @PostMapping
    public R<Void> add(@RequestBody BlogCategory category) {
        if (categoryService.exists(Wrappers.<BlogCategory>lambdaQuery().eq(BlogCategory::getName,category.getName()))) {
            return R.fail("新增分类'" + category.getName() + "'失败，类型已存在");
        }
//        categoryService.save(category);
        mapper.insert(category);
        return R.ok();
    }
    @GetMapping("optionselect")
    public R<List<BlogCategory>> optionselect(){
        List<BlogCategory> list = categoryService.list();
        return R.ok(list);
    }
}
